//
//  NSMutableDictionary+Python.m
//  CommonTools
//
//  Created by caoyuan on 5/24/16.
//  Copyright © 2016 baidu. All rights reserved.
//

#import "NSMutableDictionary+Python.h"

@implementation NSMutableDictionary (Python)


- (id)keyForObject:(id )object {
    __block id res = nil;
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj isEqual:object]) {
            res = key;
        }
    }];
    return res;
    
}

- (id (^)(id value))pyKeyFromValue {
    return ^(id value) {
        
        return [self keyForObject:value];
    };
}





- (BOOL)hasKey:(id )key {
    __block BOOL res = NO;
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull _key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([_key isEqual:key]) {
            res = YES;
        }
    }];
    return res;
    
}

- (void)forEach: (void (^)(id key, id value))block {
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        block(key, obj);
    }];
    //    NSArray *keys;
    //    NSInteger i, count;
    //    id key, value;
    //
    //    keys = [self allKeys];
    //    count = [keys count];
    //    for (i = 0; i < count; i++)
    //    {
    //        key = [keys objectAtIndex: i];
    //        value = [self objectForKey: key];
    //        block(key, value);
    //    }
}


- (BOOL)hasValue:(id)value {
    __block BOOL res = NO;
    [self forEach:^(id key, id _value) {
        if ([_value isEqual:value]) {
            res = YES;
        }
    }];
    return res;
}


- (BOOL (^)(id key))pyHasKey {
    return ^(id key) {
        
        return [self hasKey:key];
        
    };
}

- (BOOL (^)(id value))pyHasValue {
    return ^(id value) {
        
        return [self hasValue:value];
        
    };
}

@end
